/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.isystem.autpart.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.isystem.autpart.beans.TControlModulo;
import org.isystem.autpart.beans.TControlRol;
import org.isystem.autpart.beans.TCuenta;
import org.isystem.autpart.dao.DAOGeneral;
import org.isystem.autpart.util.Fecha;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Div;
import org.zkoss.zul.GroupsModel;
import org.zkoss.zul.Image;
import org.zkoss.zul.Include;
import org.zkoss.zul.Label;
import org.zkoss.zul.Separator;
import org.zkoss.zul.SimpleGroupsModel;
import org.zkoss.zul.Window;

/**
 *
 * @author rvb
 */
public class managerIndex extends GenericForwardComposer {

    Window win;
    Div divMenu;
    Div divInclude;
    Image iMenu;
    Image addUs;
//    Include xcontents;
    Label user, cargo, lb_fecha, lb_hora;
    GroupsModel modelUs;
    List<TCuenta> usuarios;
    Window winAddUs;
    public SecurityContext context;

    public void onCreate$win() {
        context = SecurityContextHolder.getContext();
        Authentication authen = context.getAuthentication();
        Object principal = null;
        if (authen != null) {
            principal = authen.getPrincipal();
        }
        String login = (String) ((org.acegisecurity.userdetails.User) principal).getUsername();
        DAOGeneral dao = new DAOGeneral();
        TCuenta cuenta = (TCuenta) dao.findAll("from TCuenta where usuario ='" + login + "'").get(0);
        user.setValue(cuenta.getTEmpleado().getTPersona().getTPersonaNatural().getNombres() + " " + cuenta.getTEmpleado().getTPersona().getTPersonaNatural().getApellidos());
        TControlRol rol = (TControlRol) dao.findAll("from TControlRol where TCuenta.id='" + cuenta.getId() + "'").get(0);
        cargo.setValue(rol.getTRol().getDenominacion());
        lb_fecha.setValue(Fecha.getFechaCompletaEsp(new Date(), false).toUpperCase());
        //CARGANDO MODULOS
        String hql_m = "from TControlModulo where  TRol.id='" + rol.getId() + "' ";

        List tcm_l = dao.findAll(hql_m);
        Iterator it = tcm_l.iterator();
        int i = 0;
        while (it.hasNext()) {
            TControlModulo tcm = (TControlModulo) it.next();
            System.out.println("modulos asignados" + tcm.getTModulo().getDenominacion());
            Image iButton = new Image();
            String img = tcm.getTModulo().getUrl().replace(".zul", ".png");
            ///autpart/img/Centigrade-Widget-Icons/admUsuario.png
            iButton.setSrc("/img/Centigrade-Widget-Icons/" + img);
            iButton.setId(tcm.getTModulo().getUrl());
            iButton.setAttribute("idxcontents", "menu" + i);
            i = i + 1;
            Separator sep = new Separator();
            sep.setBar(false);
            sep.setOrient("vertical");
            sep.setWidth("20px");
            divMenu.appendChild(iButton);
            divMenu.appendChild(sep);
            iButton.addEventListener("onClick", new EventListener() {
                public void onEvent(Event event) throws Exception {
                    Include xcontents = new Include();
                    Image iB = (Image) event.getTarget();
                    boolean existe = false;
                    for (int j = 0; j < divInclude.getChildren().size(); j++) {
                        Include xcont = (Include) divInclude.getChildren().get(j);
                        xcont.setVisible(false);
                        System.out.println("nombre=1" + xcont.getId());
                        if (iB.getAttribute("idxcontents").toString().equals(xcont.getId())) {
                            System.out.println("existe");
                            existe = true;
                            xcontents = xcont;
                        }
                    }
                    if (existe) {
                        xcontents.setVisible(true);
                    } else {
                        xcontents.setId(iB.getAttribute("idxcontents").toString());
                        xcontents.setVisible(true);
                        xcontents.setSrc(iB.getId());
                        divInclude.appendChild(xcontents);
                    }
                    divMenu.setVisible(false);
                    divInclude.setVisible(true);
                }
            });
        }
    }

    public void onTimer$timer() {
        lb_hora.setValue(Fecha.hora_actual());
    }

    public void onClick$iMenu() {
        divMenu.setVisible(true);
        divInclude.setVisible(false);
        System.out.println("ok.... menu");
    }

    public void onClick$admUs() {
        System.out.println("Administracion Usuario");
        usuarios = new LinkedList<TCuenta>();
        modelUs = new SimpleGroupsModel(usuarios);
    }

    public void onClick$addUs() {
        System.out.println("add us");
        winAddUs.setVisible(true);
        winAddUs.doOverlapped();
        winAddUs.setMaximizable(true);
        winAddUs.setMinimizable(true);
        winAddUs.setClosable(true);
        winAddUs.setPosition("center");
    }
}